'''
https://leetcode.cn/problems/reverse-string/description/
https://programmercarl.com/0344.%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
翻转字符串
编写一个函数，其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。
不要给另外的数组分配额外的空间，你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
示例 1：输入：s = ["h","e","l","l","o"] 输出：["o","l","l","e","h"]
示例 2：输入：s = ["H","a","n","n","a","h"] 输出：["h","a","n","n","a","H"]

方法：
1、库函数 reserve
2、切片 [::-1]
3、列表推导
'''
def l344(s) -> None:
    s[::1] = s[::-1]
    #[::1] 步长1表示一个个的取元素可省略[:]
    #[::2] 隔1个取1个
    pass

def l541(s:str,k:int)->str:
    def reverse_str(list_str):
        list_str[:] = list_str[::-1]
        return list_str

    def reverse_substring(text):
        left, right = 0, len(text) - 1
        while left < right:
            text[left], text[right] = text[right], text[left]
            left += 1
            right -= 1
        return text

    list_s = list(s)
    for i in range(0,len(list_s),2*k):
        '''
            2k中前k个反转 reverse_substring(list_s[i:i+k])
            i=0 s[0:4] -> s[0:2]翻转 1234 -> 2134
            i=4 s[4:5] -> s[4:6]翻转 567 -> 657 
        '''
        print(list_s[i:i+k])
        list_s[i: i + k] = reverse_substring(list_s[i: i + k])
    return "".join(list_s)
    pass

#将数字替换成number   a1b2c3 -> anumberbnumbercnumber
#isdigit 判断是否为数字
def l54(s):
    res = list(s)
    for i in range(len(s)):
        if res[i].isdigit():
            res[i] = "number"
    return "".join(res)

def l54_1(s):
    count = 0
    l = len(s)
    for i in s:
        if i>= '0' and i <= '9':
            count += 1 #统计数字的个数
    res = [""]*(l + count*5) #结果长度
    #从后向前替换
    new = len(res)-1
    old = l - 1
    while new>=0:
        if s[old] >= '0' and s[old] <='9':
            res[new-5:new+1] = "number"   # a2bxxxxx
            new -= 6
            old -= 1
        else:
            res[new] = s[old]
            new -= 1
            old -= 1
    return "".join(res)

def l151(s):
    res = s.split()
    res[:] = res[::-1]
    print(" ".join(res))

    pass

#右旋字符串 2 abcdefg   -> fgabcde
def km55(k,s):
    def reverse_str(text):
        text[:] = text[::-1]
        return text
    res = list(s)
    res[:] = res[::-1]
    res[0:k] = reverse_str(res[0:k])
    res[k::1] = reverse_str(res[k::1])
    print(res)
    pass

if __name__ == '__main__':

    s = ["h","e","l","l","o"]
    l344(s)
    print(s)
    l541("1234567",2)
    print(l54_1("a1b"))
    l151(" the sky   is blue  ")
    km55(2,"abcdefg")
    pass